package zkdemo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;

public class ZKSessionDemo {
	static CountDownLatch latch = new CountDownLatch(1);
	
	public static void main(String[] args) throws Exception {
		/**
		 * public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher)
		 * public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher,boolean canBeReadOnly)
		 * public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher,long sessionId, byte[] sessionPasswd, boolean canBeReadOnly)
		 * connectString:
		 * 	指定zk服务器列表，多台用“，”分割，也可添加上指定根目录
		 * sessionTimeout:
		 * 	会话超时时间(毫秒)	
		 * watcher:
		 * 	事件通知处理
		 * canBeReadOnly:
		 * 	会话是否是只读模式
		 * sessionId 和 sessionPasswd:
		 * 	会话ID和会话秘钥  会话复用
		 */
		ZooKeeper zk = new ZooKeeper("192.168.10.188:2181", 5000, new Watcher() {
			@Override
			public void process(WatchedEvent event) {
				System.out.println("receive watched event:"+event);
				if(event.getState() == KeeperState.SyncConnected) {
					latch.countDown();
				}
			}
		});
		System.out.println(zk.getState());
		latch.await();
		System.out.println("zookeeper session ok");
	}
}
